package com.hantanger.weblog.util;

import java.util.concurrent.Future;

import com.google.appengine.api.memcache.AsyncMemcacheService;
import com.google.appengine.api.memcache.Expiration;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

public final class Cache {

	public static MemcacheService syncCache = MemcacheServiceFactory
			.getMemcacheService();

	public static AsyncMemcacheService asyncCache = MemcacheServiceFactory
			.getAsyncMemcacheService();
	
	
	public static Object get(Object key){
		return syncCache.get(key);
	}
	
	/**
	 * 把数据存在cache，并且设置过期时间，单位为秒
	 * @param key
	 * @param value
	 * @param time 失效时间，若小于0,则不设置失效时间
	 */
	public static void put(Object key, Object value, int time){
		if(time <= 0){
			syncCache.put(key, value);
		}else{
			syncCache.put(key, value, Expiration.byDeltaSeconds(time));
		}
	}
		
	public static Future<Object> getAsyn(Object key){
		return asyncCache.get(key);
	}
	
	public static void putAsyn(Object key, Object value){
		asyncCache.put(key, value);
	}

}